<?php
namespace app\admin\controller;
use app\model\WxActivity as WxActivityModel;
use think\Db;


class WxActivity extends Base
{
    public function __construct(){
        $model = new WxActivityModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $categorys = Db::name('wx_activity_category')->select();
        $categorys = arrayParamExtract($categorys,'id','name');

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'activity_category_id'=>['name'=>'分类','type'=>'select1','params'=>$categorys],
            'type'=>['name'=>'类型','type'=>'select1','params'=>$model->getTypeList()],
            'image'=>['name'=>'图片','type'=>'file'],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'activity_date'=>['name'=>'活动日期','type'=>'date'],
            'apply_date'=>['name'=>'报名截止日期','type'=>'date'],
            'apply_time'=>['name'=>'报名截止时间','type'=>'text'],
            'address'=>['name'=>'地址','type'=>'text'],
            'num'=>['name'=>'人数','type'=>'text'],
            'price'=>['name'=>'费用','type'=>'text','placeholder'=>'0 代表免费'],
            'description'=>['name'=>'描述','type'=>'textarea'],
            'images'=>['name'=>'多图','type'=>'files'],
            'tags'=>['name'=>'标签','type'=>'text','placeholder'=>'多个用 , 分割'],
            'review_type'=>['name'=>'活动回顾','type'=>'select1','params'=>$model->getReviewTypeList()],
            'review'=>['name'=>'活动回顾 - 链接','type'=>'text'],
            'review_content'=>['name'=>'活动回顾 - 富文本','type'=>'ueditor'],
            'view_num'=>['name'=>'浏览数','type'=>'text'],
            'like_num'=>['name'=>'点赞数','type'=>'text'],
            'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
            
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
                ['key'=>'type','name'=>'类型','type'=>'select1_1','params'=>$model->getTypeList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'type','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['category'] = Db::name('wx_activity_category')->field('name')->where('id',$vo['activity_category_id'])->find();
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','activity_category_id'],
                'nomust'=>['image','images','description','address','activity_date','apply_date','apply_time','num','price','content','sort','type','view_num','like_num','tags','review','review_content','review_type','status']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','activity_category_id'],
                'nomust'=>['image','images','description','address','activity_date','apply_date','apply_time','num','price','content','sort','type','view_num','like_num','tags','review','review_content','review_type','status']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }

    /**
     * 生成二维码
     * @Author   CCH
     * @DateTime 2020-06-07T17:25:21+0800
     * @param    $path  路径
     * @param    $width 宽度
     * @return   返回图片base数据给前端小程序，直接放前端img src即可显示
     */
    public function createQrCode($path,$width=150){
        $config = config('config.wxparam');
        $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$config['appid'].'&secret='.$config['secret'];
        $access_token = httpRequest( $access_token_url );
        $access_token = json_decode($access_token,true); 
        if (empty($access_token['access_token'])) {
            return apiResult('5000','access_token获取失败');
        }

        $qcode ="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token['access_token'];
        $param = json_encode(array("path"=>$path,"width"=> 150));
        $result = httpRequest( $qcode, $param , "POST");
        if (!empty($result)) {

            return apiResult('2000','二维码生成成功',"data:image/jpeg;base64,".base64_encode( $result ));
        }else{
            return apiResult('5000','二维码生成失败');
        }
    }




   
}
